package com.fingard.dsp.bank.directbank.nnbill01.util;

import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.codec.binary.Base64OutputStream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPUtil {
    public static byte[] gzip(String s, String encode) {
        byte[] b = (byte[]) null;
        ByteArrayOutputStream bo = null;
        GZIPOutputStream gzipo = null;
        try {
            bo = new ByteArrayOutputStream();
            gzipo = new GZIPOutputStream(bo);
            byte[] b1 = s.getBytes(encode);
            gzipo.write(b1);
            gzipo.finish();
            b = bo.toByteArray();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(gzipo != null)
                    gzipo.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if(bo != null)
                    bo.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return b;
    }

    public static String unGzip(byte[] b, String encode) {
        ByteArrayInputStream bi = null;
        GZIPInputStream gzipi = null;
        byte[] bBuf = new byte[4096];
        StringBuffer buf = new StringBuffer();
        try {
            bi = new ByteArrayInputStream(b);
            gzipi = new GZIPInputStream(bi);
            int i = 0;
            while ((i = gzipi.read(bBuf)) != -1)
                buf.append(new String(bBuf, 0, i, encode));
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(gzipi != null)
                    gzipi.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if(bi != null)
                    bi.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new String(buf);
    }

    // ------------------------------------------
    public static String gzip2(String s, String encode) {
        ByteArrayOutputStream bos = null;
        GZIPOutputStream gout = null;
        Base64OutputStream b64os = null;
        try {
            bos = new ByteArrayOutputStream();
            b64os = new Base64OutputStream(bos);
            gout = new GZIPOutputStream(b64os);
            gout.write(s.getBytes(encode));
            gout.close();
            byte[] b = bos.toByteArray();
            bos.close();
            return new String(b, encode);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(gout != null) {
                    gout.close();
                }
                if(b64os != null) {
                    b64os.close();
                }
                if(bos != null) {
                    bos.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //
    public static String unGzip2(String s, String encode) {
        ByteArrayInputStream bis = null;
        GZIPInputStream gis = null;
        Base64InputStream b64is = null;
        byte[] bBuf = new byte[4096];
        StringBuffer buf = new StringBuffer();
        try {
            bis = new ByteArrayInputStream(s.getBytes(encode));
            b64is = new Base64InputStream(bis);
            gis = new GZIPInputStream(b64is);
            int i = 0;
            while ((i = gis.read(bBuf)) != -1)
                buf.append(new String(bBuf, 0, i, encode));
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(gis != null) {
                    gis.close();
                }
                if(b64is != null) {
                    b64is.close();
                }
                if(bis != null) {
                    bis.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return buf.toString();
    }

    public static void main(String[] args) {
        // System.out.println(unGzip2(gzip2("asdasd", "UTF-8"), "UTF-8"));
        System.out.println(unGzip2("H4sIAAAAAAAAAIuOBQApu0wNAgAAAA==", "UTF-8"));
        System.out.println(unGzip2("H4sIAAAAAAAAAIuOBQApu0wNAgAAAA==", "UTF-8"));
    }
}